import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;

import java.util.Arrays;
import java.util.Locale;

/**
 * @User: vitobo
 * @Date: 2024-05-27
 * @Description: String类学习
 *
 *  String类, Java当中, 字符串类型
 *
 */

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {

    // 字符串截取
    public static void main(String[] args) {
        String s = "dasjgkllfajfdsiouiewjfn";
        System.out.println(s.substring(5));
        System.out.println(s.substring(5,10));

    }

    // 字符串拆分
    public static void main9(String[] args) {
        String str = "welcome to shanxi xian";
        String[] ret = str.split(" ");
        for(String s: ret){
            System.out.println(s);
        }

        String str2 = "welcome to shanxi xian";
        String[] ret2 = str.split(" ", 2);
        for(String s : ret2){
            System.out.println(s);
        }

        // 特殊字符的拆分, 使用转义字符
        String ss = "127.0.0.1";
        String[] ret3 = ss.split("\\.");
        for(String s : ret3){
            System.out.print(s + " ");
        }
        System.out.println();


        // 多次拆分
        String sss = "name=bobo&age=25";
        String[] ret4 = sss.split("&");
        for (int i = 0; i < ret4.length; i++) {
            String[] tmp = ret4[i].split("=");
            System.out.println(tmp[0] + " = " + tmp[1]);
        }
    }

    // 字符串替换
    public static void main8(String[] args) {
        String str = "hello world";
        System.out.println(str.replace("l", "-"));
        System.out.println(str.replaceFirst("l", "-"));
    }


    // 转化
    public static void main7(String[] args) {
        // 数字转字符串
        String s1 = String.valueOf(123);
        String s2 = String.valueOf('a');
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("meizi", 22));

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println("=========================");

        // 字符串转数字
        int data1 = Integer.parseInt("456");
        double data2 = Double.parseDouble("67.54");
        System.out.println(data1);
        System.out.println(data2);
        System.out.println("=========================");

        // 大小写转换
        String ss1 = "hello";
        String ss2 = "WorLD";
        System.out.println(ss1.toUpperCase());
        System.out.println(ss2.toLowerCase());

        System.out.println("=========================");
        // 字符串转数组
        String sss1 = "hello";
        char[] ch = sss1.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
        System.out.println();

        // 数组转为字符串
        String sss2 = new String(ch);
        System.out.println(sss2);
        System.out.println("=========================");

        // 格式化输出
        String ssss = String.format("%d-%d-%d", 2024, 5, 28);
        System.out.println(ssss);
    }

    // 字符串查找
    public static void main6(String[] args) {
        String s = "aaabbbcccabcabcabcabcacbbbccc";
        System.out.println(s.charAt(3));
        System.out.println(s.indexOf('c'));
        System.out.println(s.indexOf('c', 10));
        System.out.println(s.indexOf("abc"));
        System.out.println(s.indexOf("abc", 10));
        System.out.println(s.lastIndexOf('c'));
        System.out.println(s.lastIndexOf('c', 10));
        System.out.println(s.lastIndexOf("abc"));
        System.out.println(s.lastIndexOf("abc", 10));
    }

    public static void main5(String[] args) {
        // int compareTo(String s) 方法:按照字典序进行比较
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcdef");

        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.compareTo(s4));

        String s5 = new String("abc");
        String s6 = new String("ac");
        String s7 = new String("ABc");
        String s8 = new String("abcedf");
        System.out.println(s5.compareToIgnoreCase(s6));
        System.out.println(s5.compareToIgnoreCase(s7));
        System.out.println(s5.compareToIgnoreCase(s8));
    }

    public static void main4(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");

        System.out.println(s1==s2);
        System.out.println(s1==s3);

        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));

    }

    // String对象的比较
    public static void main3(String[] args) {
        int a = 10;
        int b = 20;
        int c = 10;
        System.out.println(a==b);
        System.out.println(a==c);

        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;

        System.out.println(s1==s2);
        System.out.println(s2==s3);
        System.out.println(s1==s4);
    }

    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;

        System.out.println(s1.length());
        System.out.println(s1.isEmpty());

        System.out.println("welcome".length());
    }

    // 1.字符串构造
    public static void main1(String[] args) {
        String s1 = "hello world";
        System.out.println(s1);

        String s2 = new String("hahaha");
        System.out.println(s2);

        char[] array = {'a', 'b', 'c', 'd'};
        String s3 = new String(array);
        System.out.println(s3);

    }


}
